package leetcode;

import java.util.Stack;

public class Leetcode230 {
    public int kthSmallest(TreeNode root, int k) {
        if(root==null){
            return -1;
        }
        int count = 0;
        Stack<TreeNode> s = new Stack<>();
        while(!s.isEmpty()||root!=null){
            while(root!=null){
                s.push(root);
                root = root.left;
            }
            TreeNode pop = s.pop();
            count++;
            if(count==k){
                return pop.val;
            }
            if (pop.right!=null){
                root = pop.right;
            }
        }
        return -1;
    }
}
